package com.keiou.drink.glide;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.SimpleTarget;

/**
 * Created by Bob on 2017/2/17
 */
public class GlideUtil {

    /**
     * 默认加载
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImage(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(path).into(mImageView);
    }

    /**
     * 加载指定大小
     *
     * @param mContext
     * @param path
     * @param width
     * @param height
     * @param mImageView
     */
    public static void loadImage(Context mContext, String path, int width, int height, ImageView mImageView) {
        Glide.with(mContext).load(path).override(width, height).into(mImageView);
    }


    /**
     * 设置加载中以及加载失败图片
     *
     * @param mContext
     * @param path
     * @param mImageView
     * @param lodingImage
     * @param errorImageView
     */
    public static void loadImage(Context mContext, String path, ImageView mImageView, int lodingImage, int errorImageView) {
        Glide.with(mContext).load(path).placeholder(lodingImage).error(errorImageView).into(mImageView);
    }

    public static void loadImage(Context mContext, String path, ImageView mImageView, float thumbnail, int lodingImage, int errorImageView) {
        Glide.with(mContext).load(path).placeholder(lodingImage).error(errorImageView).thumbnail(thumbnail).into(mImageView);
    }

    /**
     * 设置加载中以及加载失败图片并且指定大小
     *
     * @param mContext
     * @param path
     * @param width
     * @param height
     * @param mImageView
     * @param lodingImage
     * @param errorImageView
     */
    public static void loadImage(Context mContext, String path, int width, int height, ImageView mImageView, int lodingImage, int errorImageView) {
        Glide.with(mContext).load(path).override(width, height).placeholder(lodingImage).error(errorImageView).into(mImageView);
    }


    /**
     * 设置跳过缓存
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImageNoCache(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(path).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(mImageView);
    }


    /**
     * 设置加载动画
     *
     * @param mContext
     * @param path
     * @param anim
     * @param mImageView
     */
    public static void loadImageAnim(Context mContext, String path, int anim, ImageView mImageView) {
        Glide.with(mContext).load(path).animate(anim).into(mImageView);
    }

    /**
     * 设置缩略图支持
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImageThumbnail(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(path).thumbnail(0.1f).into(mImageView);
    }


    /**
     * centerCrop()、fitCenter()
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImagefitCrop(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(path).fitCenter().into(mImageView);
    }


    public static void loadImageCrop(Context mContext, String path, ImageView mImageView, int placeHolderRes) {
        Glide.with(mContext).load(path).centerCrop().placeholder(placeHolderRes).error(placeHolderRes).into(mImageView);
    }

    public static void loadImageCrop(Context mContext, int res, ImageView mImageView, int placeHolderRes) {
        Glide.with(mContext).load(res).centerCrop().placeholder(placeHolderRes).error(placeHolderRes).into(mImageView);
    }

    public static void loadImageCropRound(Context mContext, String path, ImageView mImageView, int placeHolderRes) {
        Glide.with(mContext).load(path).transform(new CenterCrop(mContext),new GlideRoundTransform(mContext,6)).placeholder(placeHolderRes).error(placeHolderRes).into(mImageView);
    }


    public static void loadImageTarget(Context mContext, String path, SimpleTarget<GlideDrawable> simpleTarget) {
        Glide.with(mContext).load(path).centerCrop().into(simpleTarget);
    }

    /**
     * 移除target
     *
     * @param simpleTarget
     */
    public static void clear(SimpleTarget simpleTarget) {
        Glide.clear(simpleTarget);
    }


    /**
     * 理磁盘缓存 需要在子线程中执行
     *
     * @param mContext
     */
    public static void clearDiskCache(Context mContext) {
        Glide.get(mContext).clearDiskCache();
    }

    /**
     * 清理内存缓存  可以在UI主线程中进行
     *
     * @param mContext
     */
    public static void clearMemory(Context mContext) {
        Glide.get(mContext).clearMemory();
    }


}
